Trade Management

Getting a List of all Open Trades

get_trades(self, account_id, **params)


In [1]:
from datetime import datetime, timedelta
import pandas as pd
import oandapy
import configparser

config = configparser.ConfigParser()
config.read('../config/config_v1.ini')
account_id = config['oanda']['account_id']
api_key = config['oanda']['api_key']

oanda = oandapy.API(environment="practice", 
                    access_token=api_key)

In [2]:
trade_expire = datetime.now() + timedelta(days=1)
trade_expire = trade_expire.isoformat("T") + "Z"
fx_list = ["EUR_USD", "USD_CHF", "GBP_USD"]
for oo in fx_list:
    response = oanda.create_order(account_id,
                                  instrument = oo,
                                  units=1000,
                                  side="buy",
                                  type="market",
                                  expiry=trade_expire)

In [3]:
response = oanda.get_trades(account_id)
pd.DataFrame((response['trades']))


Out[3]:
id instrument price side stopLoss takeProfit time trailingAmount trailingStop units
0 10618881939 GBP_USD 1.25438 buy 0 0 2017-01-27T13:54:39.000000Z 0 0 1000
1 10618881930 USD_CHF 0.99977 buy 0 0 2017-01-27T13:54:39.000000Z 0 0 1000
2 10618881925 EUR_USD 1.06951 buy 0 0 2017-01-27T13:54:38.000000Z 0 0 1000
3 10611191617 NZD_USD 0.72067 buy 0 0 2017-01-20T02:34:16.000000Z 0 0 1000
4 10611191610 AUD_USD 0.75721 buy 0 0 2017-01-20T02:34:15.000000Z 0 0 1000
5 10611191585 USD_CHF 1.00533 buy 0 0 2017-01-20T02:34:09.000000Z 0 0 1000
6 10611191583 EUR_USD 1.06759 buy 0 0 2017-01-20T02:34:09.000000Z 0 0 1000
7 10609077009 NZD_USD 0.71961 buy 0 0 2017-01-18T14:41:55.000000Z 0 0 1000
8 10609077007 AUD_USD 0.75595 buy 0 0 2017-01-18T14:41:54.000000Z 0 0 1000
9 10609053339 USD_CHF 1.00321 buy 0 0 2017-01-18T14:19:11.000000Z 0 0 1000
10 10609053331 EUR_USD 1.06749 buy 0 0 2017-01-18T14:19:11.000000Z 0 0 1000
11 10605781394 NZD_USD 0.70990 buy 0 0 2017-01-16T06:17:07.000000Z 0 0 1000
12 10605781363 AUD_USD 0.74773 buy 0 0 2017-01-16T06:17:06.000000Z 0 0 1000
13 10605779828 USD_CHF 1.01029 buy 0 0 2017-01-16T06:14:26.000000Z 0 0 1000
14 10605779827 EUR_USD 1.06231 buy 0 0 2017-01-16T06:14:26.000000Z 0 0 1000
15 10605693213 NZD_USD 0.70978 buy 0 0 2017-01-16T04:33:54.000000Z 0 0 1000
16 10605693210 AUD_USD 0.74774 buy 0 0 2017-01-16T04:33:53.000000Z 0 0 1000
17 10605693150 USD_CHF 1.01112 buy 0 0 2017-01-16T04:33:47.000000Z 0 0 1000
18 10605693148 EUR_USD 1.06129 buy 0 0 2017-01-16T04:33:47.000000Z 0 0 1000
19 10605688720 USD_CHF 1.01109 buy 0 0 2017-01-16T04:28:34.000000Z 0 0 1000
20 10605688714 EUR_USD 1.06109 buy 0 0 2017-01-16T04:28:34.000000Z 0 0 1000
21 10605687742 GBP_USD 1.20372 buy 0 0 2017-01-16T04:28:16.000000Z 0 0 1000
22 10605687736 USD_CHF 1.01110 buy 0 0 2017-01-16T04:28:16.000000Z 0 0 1000
23 10605687731 EUR_USD 1.06106 buy 0 0 2017-01-16T04:28:15.000000Z 0 0 1000
24 10605686469 NZD_USD 0.70952 buy 0 0 2017-01-16T04:26:47.000000Z 0 0 1000
25 10605686468 AUD_USD 0.74762 buy 0 0 2017-01-16T04:26:47.000000Z 0 0 1000
26 10605685548 AUD_USD 0.74762 buy 0 0 2017-01-16T04:25:03.000000Z 0 0 1000
27 10605685237 AUD_USD 0.74761 buy 0 0 2017-01-16T04:24:35.000000Z 0 0 1000

In [4]:
trade_id = response['trades'][0]['id']

Get Specific Trade Information

get_trade(self, account_id, trade_id, **params)


In [5]:
response = oanda.get_trade(account_id,trade_id=trade_id)
print(response)


{'price': 1.25438, 'units': 1000, 'side': 'buy', 'time': '2017-01-27T13:54:39.000000Z', 'trailingAmount': 0, 'takeProfit': 0, 'id': 10618881939, 'stopLoss': 0, 'instrument': 'GBP_USD', 'trailingStop': 0}

You can also obtain modify trade and close trade by calling on the following APIs:

modify_trade(self, account_id, trade_id, **params)

close_trade(self, account_id, trade_id, **params)

Modify Trade

modify_trade(self, account_id, trade_id, **params)


In [6]:
response = oanda.modify_trade(account_id,trade_id=trade_id, stopLoss=1.15)

print(response)


{'price': 1.25438, 'units': 1000, 'side': 'buy', 'time': '2017-01-27T13:54:39.000000Z', 'trailingAmount': 0, 'takeProfit': 0, 'id': 10618881939, 'stopLoss': 1.15, 'instrument': 'GBP_USD', 'trailingStop': 0}

Close An Open Trade

close_trade(self, account_id, trade_id, **params)


In [7]:
response = oanda.close_trade(account_id, instrument='EUR_USD', 
                             trade_id=trade_id)
print(response)


{'price': 1.2542, 'side': 'buy', 'time': '2017-01-27T13:54:40.000000Z', 'id': 10618881962, 'profit': -0.2572, 'instrument': 'GBP_USD'}

In [8]:
response = oanda.get_trades(account_id)
pd.DataFrame(response['trades'])


Out[8]:
id instrument price side stopLoss takeProfit time trailingAmount trailingStop units
0 10618881930 USD_CHF 0.99977 buy 0 0 2017-01-27T13:54:39.000000Z 0 0 1000
1 10618881925 EUR_USD 1.06951 buy 0 0 2017-01-27T13:54:38.000000Z 0 0 1000
2 10611191617 NZD_USD 0.72067 buy 0 0 2017-01-20T02:34:16.000000Z 0 0 1000
3 10611191610 AUD_USD 0.75721 buy 0 0 2017-01-20T02:34:15.000000Z 0 0 1000
4 10611191585 USD_CHF 1.00533 buy 0 0 2017-01-20T02:34:09.000000Z 0 0 1000
5 10611191583 EUR_USD 1.06759 buy 0 0 2017-01-20T02:34:09.000000Z 0 0 1000
6 10609077009 NZD_USD 0.71961 buy 0 0 2017-01-18T14:41:55.000000Z 0 0 1000
7 10609077007 AUD_USD 0.75595 buy 0 0 2017-01-18T14:41:54.000000Z 0 0 1000
8 10609053339 USD_CHF 1.00321 buy 0 0 2017-01-18T14:19:11.000000Z 0 0 1000
9 10609053331 EUR_USD 1.06749 buy 0 0 2017-01-18T14:19:11.000000Z 0 0 1000
10 10605781394 NZD_USD 0.70990 buy 0 0 2017-01-16T06:17:07.000000Z 0 0 1000
11 10605781363 AUD_USD 0.74773 buy 0 0 2017-01-16T06:17:06.000000Z 0 0 1000
12 10605779828 USD_CHF 1.01029 buy 0 0 2017-01-16T06:14:26.000000Z 0 0 1000
13 10605779827 EUR_USD 1.06231 buy 0 0 2017-01-16T06:14:26.000000Z 0 0 1000
14 10605693213 NZD_USD 0.70978 buy 0 0 2017-01-16T04:33:54.000000Z 0 0 1000
15 10605693210 AUD_USD 0.74774 buy 0 0 2017-01-16T04:33:53.000000Z 0 0 1000
16 10605693150 USD_CHF 1.01112 buy 0 0 2017-01-16T04:33:47.000000Z 0 0 1000
17 10605693148 EUR_USD 1.06129 buy 0 0 2017-01-16T04:33:47.000000Z 0 0 1000
18 10605688720 USD_CHF 1.01109 buy 0 0 2017-01-16T04:28:34.000000Z 0 0 1000
19 10605688714 EUR_USD 1.06109 buy 0 0 2017-01-16T04:28:34.000000Z 0 0 1000
20 10605687742 GBP_USD 1.20372 buy 0 0 2017-01-16T04:28:16.000000Z 0 0 1000
21 10605687736 USD_CHF 1.01110 buy 0 0 2017-01-16T04:28:16.000000Z 0 0 1000
22 10605687731 EUR_USD 1.06106 buy 0 0 2017-01-16T04:28:15.000000Z 0 0 1000
23 10605686469 NZD_USD 0.70952 buy 0 0 2017-01-16T04:26:47.000000Z 0 0 1000
24 10605686468 AUD_USD 0.74762 buy 0 0 2017-01-16T04:26:47.000000Z 0 0 1000
25 10605685548 AUD_USD 0.74762 buy 0 0 2017-01-16T04:25:03.000000Z 0 0 1000
26 10605685237 AUD_USD 0.74761 buy 0 0 2017-01-16T04:24:35.000000Z 0 0 1000